package com.akl.ssmspringboot.service.impl;

import com.akl.ssmspringboot.service.AsyncService;
import com.akl.ssmspringboot.utils.Building;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncServiceImpl implements AsyncService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 异步方法
     * 有@Async注解的方法，默认就是异步执行的，会在默认的线程池中执行，但是此方法不能在本类调用；启动类需添加直接开启异步执行@EnableAsync。
     * */
    @Async
    @Override
    public String getTest1(){
        Building building = new Building();
        synchronized (building){
            try {
                for (int i = 1;i <= 10;i++){
                    logger.info(Thread.currentThread().getName()+"----------异步：>"+i);
                    building.wait(2000);
                }
                return "执行异步任务完毕";
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return Thread.currentThread().getName()+"执行完毕";
    }
}
